package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.BaseData;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.service.IBaseDataService;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/BaseDataController")
public class BaseDataController  extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(BaseDataController.class);

    @Autowired
    private IBaseDataService service;
    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    /**
     * 查询 工资项基础数据分页
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/querySalaryTypeBaseDataPage")
    public ApiResultDTO querySalaryTypeBaseDataPage(@RequestHeader("X-Token") String tokenid,
                                                @RequestParam(value = "page") String page,
                                                @RequestParam(value = "limit") String limit) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam=new HashMap<>();
            queryParam.put("mof_div_code",userDTO.getProvince());
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<BaseData> pageDTO = service.querySalaryTypeBaseDataPage(paginationDTO,queryParam);
            return ApiResultDTO.success(pageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询异常");
        }
    }

    /**
     * 新增 工资类型基础数据
     * @param baseData
     * @return
     */
    @PostMapping("/saveSalaryTypeBaseData")
    public ApiResultDTO saveSalaryTypeBaseData(@RequestHeader("X-Token") String tokenid,
                                               @RequestBody BaseData baseData) {
        try {
            UserDTO userDTO = getUser(tokenid);
            service.saveSalaryTypeBaseData(baseData,userDTO);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 工资类型基础数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE,  e.getMessage());
        }
    }

    /**
     * 更新 工资类型基础数据
     * @param baseData
     * @return
     */
    @PostMapping("/updateSalaryBasedataPage")
    public ApiResultDTO updateSalaryTypeBaseData(@RequestHeader("X-Token") String tokenid,
                                                 @RequestBody BaseData baseData) {
        try {
            UserDTO userDTO = getUser(tokenid);
            service.updateSalaryTypeBaseData(baseData,userDTO);
            return ApiResultDTO.success(null, "更新成功");
        } catch (Exception e) {
            logger.error("更新 工资类型基础数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }


    /**
     * 保存 人员工资类型基础数据
     * @param BaseDataList
     * @return
     */
    @PostMapping("/submitSalaryBaseData")
    public ApiResultDTO submitSalaryBaseData(@RequestHeader("X-Token") String tokenid,
                                                 @RequestBody List<BaseData> BaseDataList) {
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("工资配置管理--人员工资类型基础数据--保存");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);
            service.submitSalaryTypeBaseData(BaseDataList,userDTO);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("保存 人员工资类型基础数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

    /**
     * 工资类别删除
     * @param baseData
     * @return
     */
    @PostMapping("/deleteSalaryType")
    public ApiResultDTO deleteSalaryType(@RequestHeader("X-Token") String tokenid,
                                                 @RequestBody BaseData baseData) {
        try {
            UserDTO userDTO = getUser(tokenid);
           service.deleteSalaryType(baseData,userDTO);
            return ApiResultDTO.success(null, "更新成功");
        } catch (Exception e) {
            logger.error("更新 工资类型基础数据发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }



}

